﻿//New Charge ViewModel  
//author: Alexis 2011-06-25
//blog: http://www.cnblogs.com/alexis/

using System;
using MvvmLight;
using MvvmLight.Command;
using AccountHelper.Model;
using AccountHelper.Common;
using MvvmLight.Messaging;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace AccountHelper.ViewModel
{
    public class NewChargeViewModel:ViewModelBase
    {
        public NewChargeViewModel()
        {
            ChargeTypes = new List<KeyValuePair<string, ChargeType>>();
            ChargeTypes.Add(new KeyValuePair<string, ChargeType>("支出", ChargeType.Cost));
            ChargeTypes.Add(new KeyValuePair<string, ChargeType>("收入", ChargeType.Charge));

            DtDate = DateTime.Now;
            InitChargeTitles();
        }

        #region Property

        //费用类型，目前为收入、支出两种
        public List<KeyValuePair<string, ChargeType>> ChargeTypes { get; set; }

        //常用的费用标题
        public ObservableCollection<string> CommonChargeTitles { get; set; }
             
        //编辑时候用的id
        public int ChargeId { get; set; }

        private string chargeTitle;
        public string ChargeTitle
        {
            get
            {
                return chargeTitle;
            }
            set
            {
                chargeTitle = value;
                RaisePropertyChanged("ChargeTitle");
            }
        }

        private KeyValuePair<string, ChargeType> selectType = new KeyValuePair<string, ChargeType>("支出", ChargeType.Cost);
        public KeyValuePair<string, ChargeType> SelectType
        {
            get
            {
                return selectType;
            }
            set
            {
                selectType = value;
                RaisePropertyChanged("SelectType");
            }
        }

        private DateTime dtDate;
        public DateTime DtDate
        {
            get 
            {
                return dtDate; 
            }
            set 
            { 
                dtDate = value; 
                RaisePropertyChanged("DtDate"); 
            }
        }

        private double mAmount;
        public double MAmount
        {
            get { return mAmount; }
            set
            {
                mAmount = value;
                RaisePropertyChanged("MAmount");
            }
        }

        private string mDescript;
        public string MDescript
        {
            get { return mDescript; }
            set
            {
                mDescript = value;
                RaisePropertyChanged("MDescript");
            }
        }
        #endregion

        #region Command

        private RelayCommand saveCommand;
        public RelayCommand SaveCommand
        {
            get
            {
                return saveCommand ?? (saveCommand = new RelayCommand(() =>
                {
                    if (this.ChargeId!=0)//edit
                    {
                        Money m = new Money
                        {
                            MoneyId = this.ChargeId,
                            ChargeTitle = this.ChargeTitle,
                            Type = SelectType.Value,
                            Amount = this.MAmount,
                            ChargeDate = this.DtDate.ToString(),
                        };
                        ViewModelLocator.Instance.MainVM.UpdateCommand.Execute(m);
                    }
                    else
                    {
                        Money m = new Money
                        {
                            ChargeTitle = this.ChargeTitle,
                            Type = SelectType.Value,
                            Amount = this.MAmount,
                            ChargeDate = this.DtDate.ToString(),
                        };
                        ViewModelLocator.Instance.MainVM.AddNewMoneyCommand.Execute(m);
                    }
                    
                    Messenger.Default.Send<bool>(true, "GoBack");
                }));
            }
        }

        private RelayCommand resetCommand;
        public RelayCommand ResetCommand
        {
            get
            {
                return resetCommand ?? (resetCommand = new RelayCommand(() =>
                {
                    this.ChargeTitle = "";
                    this.MAmount = 0;
                    this.ChargeId = 0;
                }));
            }
        }

        private void InitChargeTitles()
        {
            if (CommonChargeTitles==null)
            {
                CommonChargeTitles = new ObservableCollection<string>();
            }
            //ToDo : save common used charge title , top tp 20
            CommonChargeTitles.Add("今天我吃饭花了");
            CommonChargeTitles.Add("超市购物");

        }
        #endregion
        
        
    }
}
